<template>
  <div class="outside">
    <div class="app-container" style="width: 92%">
      <inline-form
        ref="InlineForm"
        :item="item"
        @doSubmit="handleDoSubmit"
      ></inline-form>
    </div>
  </div>
</template>

<script>
import _form from "./_form";
import { addImage } from "../../../utils/common";
import { add } from "./api/index";
export default {
  data() {
    return {
      item: {
        image: "",
        imageFile: "",
      },
    };
  },
  methods: {
    async handleDoSubmit() {
      const form = this.$refs.InlineForm;
      if (!form) return;
      form.valid(async (valid) => {
        if (valid) {
          const item = form.obj;
          console.log(item, "有吗？");
          if (
            item.imageFile &&
            item.imageFile !== "" &&
            item.imageFile !== "img_none"
          ) {
            item.image = await addImage(item.imageFile);
          }
          delete item.imageFile;
          const res = await add(item.image);
          console.log(res, "====>");
          if (res.code === 1) {
            this.$message.success("添加成功!");
            this.$router.go(-1);
          } else {
            this.$message.error(res.msg);
          }
        } else {
          this.$message.error("数据填写错误，请按要求填写!");
        }
      });
    },
  },
  components: {
    "inline-form": _form,
  },
};
</script>

<style scoped></style>
